using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Xml;

//using Microsoft.SqlServer.Management.Smo;

namespace GiangDay
{
    public partial class FrServer : DevComponents.DotNetBar.Office2007Form
    {
        public FrServer()
        {
            InitializeComponent();
        }

        DB data = new DB();
        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Dropdown Commbo Server
        private void cb_server_DropDown(object sender, EventArgs e)
        {
            //DataTable datatable = SmoApplication.EnumAvailableSqlServers(false); //true: local, false all
            DataTable datatable = SqlDataSourceEnumerator.Instance.GetDataSources();
            cb_server.Items.Clear();
            foreach (DataRow row in datatable.Rows)
            {
                cb_server.Items.Add(row["ServerName"].ToString()+"\\"+ row["InstanceName"].ToString());
            }       
        }

        private void cb_database_DropDown(object sender, EventArgs e)
        {          
            SqlConnection connection = new SqlConnection("Data Source="+ cb_server.Text +";User ID="+ tb_userid.Text.Trim() +";Password="+ tbPass.Text.Trim () +";");
            DataTable databases = null;
            try
            {
                connection.Open();
                databases = connection.GetSchema("Databases");
                cb_database.Items.Clear();
                foreach (DataRow row in databases.Rows)
                {
                    this.cb_database.Items.Add(row["database_name"].ToString());
                }      
            }
            catch (Exception)
            {
                connection.Close();
                return;
            }         
            connection.Close();           
        }

        private void bt_cancel_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrServer_Load(object sender, EventArgs e)
        {
            XmlTextReader reader = new XmlTextReader("config.xml");
            while (reader.Read())
            {
                // Do some work here on the data.
                if (reader.NodeType==XmlNodeType.Element)
                {
                   // MessageBox.Show(reader.Name + reader.Value + reader.NodeType.ToString());
                }
               
            }
            reader = null;

        }

        private void bt_login_Click(object sender, EventArgs e)
        {
            if (data.realtime()==1)
            {
                
            }
        }

        private void FrServer_FormClosed(object sender, FormClosedEventArgs e)
        {
            data = null;
            GC.SuppressFinalize(this);
        }

    }
}